import modelEnhance from '@/utils/modelEnhance';
import PageHelper from "@/utils/pageHelper";

let LOADED = false;
export default modelEnhance({
  namespace: 'examsDetail',

  state: {
    pageData: PageHelper.create(),
    publishStatus: false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/exams/detail' && !LOADED) {
          LOADED = true;
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },

  effects: {
    // 进入页面加载
    *init({payload}, {call, put, select}) {
    },
    // 获取分页数据
    *loadExamData({payload}, {call, put,select}) {
      const { pageData } = yield select(state => state.examsDetail);
      yield put({
        type: '@request',
        payload: {
          actionType: 'setExamsDeatailInfo',
          url: '/api/exam/detail/'+ payload.id ,
        }
      });
    },

    *addSelection({payload}, {call, put, select}){
      const {values, examId, success} = payload;

      values.optionInfos = [];
      ['a', 'b', 'c', 'd', 'e', 'f', 'g'].forEach(item=> {
        if (values['option_' + item]){
          values.optionInfos.push({
            isAnswer: values.correctAnswer == item.toUpperCase() ? true: false,
            name: values['option_' + item],
            optionOrder: item.toUpperCase(),
          })
        }
      });

      yield put.resolve({
        type: '@request',
        payload: {
          url: '/api/exam/examPaper/addSingleSelectionQuestion',
          data: values,
          notice: true,
        }
      });

      yield put.resolve({
        type: 'loadExamData',
        payload: {id: examId}
      });
      success();
    },

    *editSelection({payload}, {call, put, select}){
      const {values, examId, success} = payload;

      values.optionInfos = [];
      ['a', 'b', 'c', 'd', 'e', 'f', 'g'].forEach(item=> {
        if (values['option_' + item]){
          values.optionInfos.push({
            isAnswer: values.correctAnswer == item.toUpperCase() ? true: false,
            name: values['option_' + item],
            optionOrder: item.toUpperCase(),
          })
        }
      });

      yield put.resolve({
        type: '@request',
        payload: {
          url: '/api/exam/examPaper/modifySingleSelectionQuestion',
          data: values,
          notice: true,
        }
      });

      yield put.resolve({
        type: 'loadExamData',
        payload: {id: examId}
      });
      success();
    },

    *addTrue({payload}, {call, put, select}){
      const {values, examId, success} = payload;
      yield put.resolve({
        type: '@request',
        payload: {
          url: '/api/exam/examPaper/addTrueOrFalseQuestion',
          data: values,
          notice: true,
        }
      });

      yield put.resolve({
        type: 'loadExamData',
        payload: {id: examId}
      });

      success();
    },

    *editTrue({payload}, {call, put, select}){
      const {values, examId, success} = payload;
      yield put.resolve({
        type: '@request',
        payload: {
          url: '/api/exam/examPaper/modifyTrueOrFalseQuestion',
          data: values,
          notice: true,
        }
      });

      yield put.resolve({
        type: 'loadExamData',
        payload: {id: examId}
      });

      success();
    },


    *remove({payload}, {call,put, select}){
      const { examId, records, success } = payload;

      yield put.resolve({
        type: '@request',
        payload: {
          method: 'DELETE',
          url:'/api/exam/examPaper/' + examId + '/deleteQuestion',
          data: records.map(item=> item.id),
        }
      });

      yield put.resolve({
        type: 'loadExamData',
        payload: {id: examId}
      });

      success();
    }
  },

  reducers: {
    setExamsDeatailInfo_SUCCESS(state, {payload}){
      const {pageData} = state;
      console.log(payload);
      return {
        ...state,
        pageData: pageData.setPage({data: {records: payload.data.examPaperQuestionInfoVos}}),
        publishStatus: payload.data.publishStatus == 'PUBLISHED' ? true : false,
      }
    }
  }

});